/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package fileby.util.plugins.samples;

import fileby.plugins.IPluggable;
import fileby.plugins.IPluginAnnotation;
import fileby.plugins.IToolbox;
import java.io.File;
import java.util.List;

/**
 *
 * @author Ludwig Kjellstrom
 */
@IPluginAnnotation(name="Renamer", description="Renames the files, " +
"from 000 to 999 with correct extension")
public class RenamerPlugin implements IPluggable{
    public boolean run(IToolbox toolBox) {
        List<File> files = toolBox.getFiles();
        if(toolBox.getFiles().size() > 1000)
            return false;
        int i = 0;
        for (File f : files) {
            String extension = "";
            if (!f.isDirectory()) {
                if(f.getName().lastIndexOf('.') > -1)
                    extension = f.getName().substring(f.getName().lastIndexOf('.'), f.getName().length());
            }
            String newName = String.format("%03d", i++);
            if (!f.renameTo(new File(f.getParent() + "\\" + newName + extension))) {
                System.err.println(f.getAbsolutePath() + ": could not be renamed to " + f.getParent() + "\\" + newName + extension);
            }
        }
        toolBox.clearMagicBox();
        return true;
    }

}
